rm(list=ls(all=TRUE))
library(lfe)
library(questionr)
library(tidyverse)
library(stargazer)

load("federalism_data.RData")

################################################################
## Table 1: Binary levels of support for each federalism item ##
################################################################

round(prop.table(wtd.table(df_analysis$fed_q1, weights = df_analysis$wts.trim)),3) * 100
round(prop.table(wtd.table(df_analysis$fed_q2, weights = df_analysis$wts.trim)),3) * 100
round(prop.table(wtd.table(df_analysis$fed_q3, weights = df_analysis$wts.trim)),3) * 100
round(prop.table(wtd.table(df_analysis$fed_q4, weights = df_analysis$wts.trim)),3) * 100
round(prop.table(wtd.table(df_analysis$fed_q5, weights = df_analysis$wts.trim)),3) * 100
round(prop.table(wtd.table(df_analysis$fed_q6, weights = df_analysis$wts.trim)),3) * 100
round(prop.table(wtd.table(df_analysis$fed_q7, weights = df_analysis$wts.trim)),3) * 100
round(prop.table(wtd.table(df_analysis$fed_q8, weights = df_analysis$wts.trim)),3) * 100
round(prop.table(wtd.table(df_analysis$fed_q9, weights = df_analysis$wts.trim)),3) * 100
round(prop.table(wtd.table(df_analysis$fed_q10, weights = df_analysis$wts.trim)),3) * 100

#########################
## Figure 1: Histogram ##
#########################

fed.hist = ggplot(df_analysis, aes(x = fed.score.average, weight = wts.trim)) + 
		geom_histogram(color="black",fill="grey") +
		theme_bw() + labs(x = "Federalism preferences", y = "Weighted frequency") +
		geom_vline(xintercept=mean(df_analysis$fed.score.average), linetype="dashed", 
			size=2, color = "black") +
		theme(axis.title.y = element_text(margin = margin(t = 0, r = 10, b = 0, l = 0))) 
fed.hist

#####################################
## Table 2: Support for devolution ##
#####################################

education <- felm(education ~ fed.score.average + resp.is.rep + resp.is.dem + ideology + hhi + age10 + factor(race) + is.hispanic + college + gender | StateName | 0 | StateName,data=df_analysis,weights = df_analysis$wts.trim)
roads <- felm(roads ~ fed.score.average + resp.is.rep + resp.is.dem+ ideology + hhi  + age10 + factor(race)+ is.hispanic + college + gender | StateName | 0 | StateName,data=df_analysis,weights = df_analysis$wts.trim)
econ_affairs <- felm(econ_affairs ~ fed.score.average+ resp.is.rep + resp.is.dem + ideology + hhi + age10 + factor(race)+ is.hispanic + college + gender | StateName | 0 | StateName,data=df_analysis,weights = df_analysis$wts.trim)
foreign_affairs <- felm(foreign_affairs ~fed.score.average+ resp.is.rep + resp.is.dem + ideology + hhi + age10 + factor(race)+ is.hispanic + college + gender | StateName | 0 | StateName,data=df_analysis,weights = df_analysis$wts.trim)
environment <- felm(environment ~ fed.score.average + resp.is.rep + resp.is.dem+ ideology + hhi + age10 + factor(race)+ is.hispanic + college + gender |StateName | 0 | StateName,data=df_analysis,weights = df_analysis$wts.trim)
health <- felm(health ~ fed.score.average+ resp.is.rep + resp.is.dem + ideology + hhi + age10 + factor(race)+ is.hispanic + college + gender| StateName | 0 | StateName,data=df_analysis,weights = df_analysis$wts.trim)
social <- felm(social_welfare ~  fed.score.average+ resp.is.rep + resp.is.dem + ideology + hhi + age10 + factor(race)+ is.hispanic + college + gender | StateName | 0 | StateName,data=df_analysis,weights = df_analysis$wts.trim)
law <- felm(law_enforcement ~  fed.score.average+ resp.is.rep + resp.is.dem + ideology + hhi + age10 + factor(race)+ is.hispanic + college+ gender | StateName| 0 | StateName,data=df_analysis,weights = df_analysis$wts.trim)
criminal <- felm(criminal_justice ~ fed.score.average+ resp.is.rep + resp.is.dem + ideology + age10 + hhi + factor(race)+ is.hispanic + college + gender| StateName | 0 | StateName,data=df_analysis,weights = df_analysis$wts.trim)
score <- felm(score ~  fed.score.average+ resp.is.rep + resp.is.dem + ideology + hhi + age10 + factor(race)+ is.hispanic + college + gender | StateName | 0 | StateName,data=df_analysis,weights = df_analysis$wts.trim)

devolution_tab <- stargazer(education,roads,econ_affairs,foreign_affairs,environment,health,social,law,criminal,score,
                            dep.var.caption = "Policy area",
                            dep.var.labels = c("Education","Roads","Economic","Foreign","Environ","Health","Social","Law","Criminal","Composite"),
                            title="Federalism Preferences and Support for Policy Devolution",
                            label="devolution", omit="StateName",
                            covariate.labels = c("Federalism preferences", "Republican","Democrat","Ideology","Income","Age (decades)",
                                                 "Black","Asian American","Other race/ethnicity","Hispanic","College degree","Woman"),
                            add.lines = list(c("State Fixed Effects","\\checkmark","\\checkmark","\\checkmark","\\checkmark",
                                               "\\checkmark","\\checkmark","\\checkmark","\\checkmark","\\checkmark","\\checkmark")),
                            keep.stat = c("n","adj.rsq"),
                            star.cutoffs = c(0.05),star.char = c("*"),table.placement="!ht",
                            notes.append = FALSE,notes.label = "",column.sep.width="-10pt",
                            table.layout ="-ld-#-t-as-n",font.size="scriptsize",
                            notes="\\parbox[t]{0.9\\textwidth}{\\footnotesize \\textit{Note}: Entries are linear regression coefficients with 
                     standard errors clustered on states in parentheses. Specifications use survey weights constructed on the basis of population parameters obtained from the 2019 CPS. The dependent variable is whether respondents support devolution
			   for the issues listed at the top of each column. $^{*}$p$<$0.05 (two-tailed test).}")

###################################################
## Table 3: Predictors of federalism preferences ##
###################################################

fedpref <- felm(fed.score.average ~  resp.is.rep + resp.is.dem + ideology + hhi + age10 + factor(race) + is.hispanic + college+ gender| 0 | 0 | StateName,data=df_analysis,weights = df_analysis$wts.trim)
fedpref_fe <- felm(fed.score.average ~  resp.is.rep + resp.is.dem + ideology + hhi + age10 + factor(race) + is.hispanic + college+ gender| StateName | 0 | StateName,data=df_analysis,weights = df_analysis$wts.trim)

fedpref_tab <- stargazer(fedpref,fedpref_fe,
                     dep.var.caption = "DV: Federalism preferences",
                     dep.var.labels   = "",
                     title="Predictors of Federalism Preferences",
                     label="federalism_preferences", omit=c("Constant","StateName"),
			   order=c("resp.is.rep","resp.is.dem","ideology","hhi","age10","race","is.hispanic","college","gender"),
                     covariate.labels = c("Republican","Democrat","Ideology","Income","Age (decades)",
                                          "Black","Asian American","Other race/ethnicity","Hispanic","College degree","Woman"),
                     add.lines = list(c("State Fixed Effects","","\\checkmark")),
                     keep.stat = c("n","adj.rsq"),digits=3,digits.extra = 0,
                     star.cutoffs = c(0.05),star.char = c("*"),table.placement="!ht",
                     notes.append = FALSE,notes.label = "",column.sep.width="10",
                     table.layout ="-ld-#-t-as-n",font.size="footnotesize",
                     notes="\\parbox[t]{0.6\\textwidth}{\\footnotesize \\textit{Note}: Entries are linear regression coefficients with 
                     standard errors clustered on states in parentheses. Specifications use survey weights constructed on the basis of population parameters obtained from the 2019 CPS. The dependent variable is respondents' federalism preferences. $^{*}$p$<$0.05 (two-tailed test).}")

########################################################
## Table 4: Political alignment with state government ##
########################################################

align1 <- felm(fed.score.average ~ aligned + not.aligned + ideology  + hhi + age10 + factor(race) + is.hispanic + college+ gender |0 | 0 | StateName,data=df_analysis,weights = df_analysis$wts.trim)
align2 <- felm(fed.score.average ~ gov_approval + resp.is.rep + resp.is.dem + ideology  + hhi + age10 + factor(race) + is.hispanic + college+ gender |StateName | 0 | StateName,data=df_analysis,weights = df_analysis$wts.trim)
align3 <- felm(fed.score.average ~ gov_approval +  aligned + not.aligned+ ideology  + hhi + age10 + factor(race) + is.hispanic + college+ gender |StateName | 0 | StateName,data=df_analysis,weights = df_analysis$wts.trim)

align_tab <- stargazer(align1,align2,align3,
                       dep.var.caption = "DV: Federalism preferences",
                       dep.var.labels   = "",
                       title="Partisan Alignment, Gubernatorial Approval, and Preferences for Federalism",
                       label="align_tab", omit=c("Constant"),order=c("aligned","not.aligned","gov_approval","resp.is.rep","resp.is.dem",
                                                                     "ideology","hhi","age10","race","is.hispanic","college","gender"),
                       covariate.labels = c("Same-party governor","Opposite-party governor","Gubernatorial approval","Republican","Democrat","Ideology","Income",
                                            "Age (decades)", "Black","Asian American","Other race/ethnicity","Hispanic","College degree","Woman"),
                       add.lines = list(c("State Fixed Effects","\\checkmark","\\checkmark","\\checkmark")),
                       keep.stat = c("n","adj.rsq"),digits=3,digits.extra = 0,
                       star.cutoffs = c(0.05),star.char = c("*"),table.placement="!ht",
                       notes.append = FALSE,notes.label = "",column.sep.width="10",
                       table.layout ="-ld-#-t-as-n",font.size="footnotesize",
                       notes="\\parbox[t]{0.6\\textwidth}{\\footnotesize \\textit{Note}: Entries are linear regression coefficients with 
                     standard errors clustered on states in parentheses. Specifications use survey weights constructed on the basis of population parameters obtained from the 2019 CPS.  The dependent variable is respondents' federalism preferences. 
			   $^{*}$p$<$0.05 (two-tailed test).}")


##########################################################################
## Table 5: State Government Performance and Preferences for Federalism ##
##########################################################################

perf <- felm(fed.score.average ~  state_sentiment  + resp.is.rep + resp.is.dem + ideology  + hhi + age10 + factor(race) + is.hispanic + college + gender |0 | 0 | StateName,data=df_analysis,weights = df_analysis$wts.trim)
perf_fe <- felm(fed.score.average ~  state_sentiment + resp.is.rep + resp.is.dem + ideology  + hhi + age10 + factor(race) + is.hispanic + college + gender |StateName | 0 | StateName,data=df_analysis,weights = df_analysis$wts.trim)

perf_tab <- stargazer::stargazer(perf,perf_fe,
                       dep.var.caption = "DV: Federalism preferences",
                       dep.var.labels   = "",
                       title="Evaluations of State Government Performance and Preferences for Federalism",
                       label="performance_federalism", omit=c("Constant"),order=c("state_sentiment","resp.is.rep","resp.is.dem",
                                                                     "ideology","hhi","age10","race","is.hispanic","college","gender"),
                       covariate.labels = c("State job performance", "Republican","Democrat","Ideology","Income","Age (decades)",
                                            "Black","Asian American","Other race/ethnicity","Hispanic","College degree","Woman"),
                       add.lines = list(c("State Fixed Effects","","\\checkmark")),
                       keep.stat = c("n","adj.rsq"),digits=3,digits.extra = 0,
                       star.cutoffs = c(0.05),star.char = c("*"),table.placement="!ht",
                       notes.append = FALSE,notes.label = "",#column.sep.width="15",
                       table.layout ="-ld-#-t-as-n",font.size="footnotesize",
                       notes="\\parbox[t]{0.6\\textwidth}{\\footnotesize \\textit{Note}: Entries are linear regression coefficients with 
                     standard errors clustered on states in parentheses. Specifications use survey weights constructed on the basis of population parameters obtained from the 2019 CPS.    The dependent variable is respondents' federalism preferences. 
			   $^{*}$p$<$0.05 (two-tailed test).}")


